package com.mrma.handle;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.SilentCssErrorHandler;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class chrome {
 
  public static void main(String[] args) {

  }

  /**
   * 模拟示例1
   */
  public void chromLogin() {
    //下载的chromedriver位置
    System.setProperty("webdriver.chrome.driver","D:\\idea_workspace\\Jsoup\\src\\main\\chromedriver.exe");
    //实例化ChromeDriver对象
    WebDriver driver = new ChromeDriver();
    String url="https://search.51job.com/list/000000,000000,0000,00,9,99,%25E5%25A4%25A7%25E6%2595%25B0%25E6%258D%25AE%25E5%25B7%25A5%25E7%25A8%258B%25E5%25B8%2588,2,1.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=";
    //打开指定网站
    driver.get(url);
    //解析页面
    String pageSource =driver.getPageSource();
    Document jsoup = Jsoup.parse(pageSource);
    //定义选择器规则
    String rule="#resultList > div:nth-child(4) > p > span > a";
    //通过选择器拿到元素
    Elements select = jsoup.select(rule);
    String s=select.text();
    System.out.println(s);
    //模拟浏览器点击
    driver.findElement(By.cssSelector(rule)).click();
  }

  /**
   * 模拟登录示例2
   */
  public void loginGitee() {
    try (WebClient webClient = new WebClient(BrowserVersion.CHROME)) {
      /******配置webClient******/
      //ajax
      webClient.setAjaxController(new NicelyResynchronizingAjaxController());
      //支持js
      webClient.getOptions().setJavaScriptEnabled(true);
      //忽略js错误
      webClient.getOptions().setThrowExceptionOnScriptError(false);
      //忽略css错误
      webClient.setCssErrorHandler(new SilentCssErrorHandler());
      //不执行CSS渲染
      webClient.getOptions().setCssEnabled(false);
      //超时时间
      webClient.getOptions().setTimeout(3000);
      //允许重定向
      webClient.getOptions().setRedirectEnabled(true);
      //允许cookie
      webClient.getCookieManager().setCookiesEnabled(true);

      //开始请求网站
      HtmlPage page = webClient.getPage("https://gitee.com");
      //点击首页上的登陆按钮，跳转到登陆页面
      HtmlPage loginPage = ((DomElement) page.getByXPath("/html/body/header/div/div/div[5]/a[1]").get(0)).click();
      /*
       * 获取登陆表单，表单如果是依赖js或css生成的，要等待加载完成，现有框架里等待方法不完善
       * 这里可以采用循环等待的方案，等到全部资源加载完，获取到了要取的表单元素再继续执行
       */
      while (loginPage.getByXPath("/html/body/div[2]/div[2]/div/div[1]/div[2]/div/form[1]").size() == 0) {
        Thread.sleep(500);
      }
      //获取登陆表单元素
      HtmlForm form = (HtmlForm) loginPage.getByXPath("/html/body/div[2]/div[2]/div/div[1]/div[2]/div/form[1]").get(0);
      //用户名input
      HtmlTextInput username = (HtmlTextInput) form.getElementsByAttribute("input", "id", "user_login").get(0);
      //密码input
      HtmlPasswordInput password = (HtmlPasswordInput) form.getElementsByAttribute("input", "id", "user_password").get(0);
      //设置input的value
      username.setValueAttribute("*****");
      password.setValueAttribute("*****");
      //登陆
      HtmlPage home = ((DomElement) loginPage.getByXPath("/html/body/div[2]/div[2]/div/div[1]/div[2]/div/form[1]/div[2]/div/div/div[4]/input").get(0)).click();
      //搜索项目
      HtmlPage searchPage = webClient.getPage("https://gitee.com/search?utf8=%E2%9C%93&type=&fork_filter=on&q=java");
      //打印列表
      HtmlElement a = (HtmlElement) searchPage.getByXPath("/html/body/div[3]/div[1]/div/div[2]").get(0);
      System.out.println(searchPage.getBaseURL());
      System.out.println(a.getTextContent());
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}